package com.view.personagemanage;

import com.bean.User;
import com.lovo.netCRM.component.LovoButton;
import com.service.UserService;
import com.service.impl.UserServiceImpl;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PersonageManagePanel extends JPanel {
    private MainFrame frame;

    private JButton updatePwdBtn;
    private JLabel oldPwdLab;
    private JTextField oldPwdTxt;
    private JLabel newPwdLab;
    private JTextField newPwdTxt;
    private JLabel reNewPwdLab;
    private JTextField reNewPwdTxt;
    private UserService userService = new UserServiceImpl();


    public PersonageManagePanel(MainFrame frame) {
        this.frame = frame;
        this.setBackground(Color.WHITE);
        this.setLayout(null);

        this.oldPwdLab = new JLabel("旧密码:");
        this.oldPwdLab.setBounds(220, 120, 60, 20);
        this.add(oldPwdLab);
        this.oldPwdTxt = new JTextField();
        this.oldPwdTxt.setBounds(350, 120, 120, 20);
        this.add(oldPwdTxt);

        this.newPwdLab = new JLabel("新密码:");
        this.newPwdLab.setBounds(220, 220, 60, 20);
        this.add(newPwdLab);
        this.newPwdTxt = new JTextField();
        this.newPwdTxt.setBounds(350, 220, 120, 20);
        this.add(newPwdTxt);

        this.reNewPwdLab = new JLabel("确认新密码:");
        this.reNewPwdLab.setBounds(220, 320, 60, 20);
        this.add(reNewPwdLab);
        this.reNewPwdTxt = new JTextField();
        this.reNewPwdTxt.setBounds(350, 320, 120, 20);
        this.add(reNewPwdTxt);

        this.updatePwdBtn = new LovoButton("修改密码", 335, 380, this);

        updatePwdBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String oldPwdTxtText = oldPwdTxt.getText();
                String newPwdTxtText = newPwdTxt.getText();
                String reNewPwdTxtText = reNewPwdTxt.getText();
                User user = frame.getUser();
                if(userService.updatePassword(user,oldPwdTxtText,newPwdTxtText,reNewPwdTxtText) == 1){
                    JOptionPane.showMessageDialog(null,"密码不符，请输入正确的密码");
                }else if (userService.updatePassword(user,oldPwdTxtText,newPwdTxtText,reNewPwdTxtText) == 2){
                    JOptionPane.showMessageDialog(null,"第二次密码输入错误，请再次输入");
                }else if (userService.updatePassword(user,oldPwdTxtText,newPwdTxtText,reNewPwdTxtText) == 3){
                    JOptionPane.showMessageDialog(null,"修改密码成功");
                }
            }
        });
    }
}
